package com.atguigu.gmall.weball.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.feign.product.CategoryFeignClient;
import com.atguigu.gmall.product.to.CategoryTree;
import feign.Request;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.lang.reflect.Proxy;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author lfy
 * @Description 首页页面跳转控制器
 * @create 2023-08-18 10:57
 * 前后不分离： Controller
 * 前后分离： RestController
 */
@Controller
public class IndexController {


    @Autowired
    CategoryFeignClient categoryFeignClient;

    @ResponseBody
    @GetMapping("/hello")
    public String hello(){
        return "hello";
    }

    @GetMapping({"/","/index.html"})
    public String index(Model model){
        //首页

        //远程调用 service-product 获取所有数据； 发送网络请求，并把数据转为指定类型
        Request.Options options = new Request.Options(
                1, TimeUnit.SECONDS,5,TimeUnit.SECONDS,true); //默认是10s的连接超时，60s的读取超时

        //精确指定此次调用的超时时间
//        Result<List<CategoryTree>> treeData = categoryFeignClient.getCategoryTreeData(options);

        Result<List<CategoryTree>> treeData = categoryFeignClient.getCategoryTreeData();
        List<CategoryTree> data = treeData.getData();


        model.addAttribute("list",data);

        return "index/index";
    }

}
